package com.cuanbo.dualscreenteachzhu.activity;

import android.app.Activity;
import android.app.ActivityOptions;
import android.content.ActivityNotFoundException;
import android.content.ComponentName;
import android.content.Intent;
import android.content.IntentFilter;
import android.hardware.display.DisplayManager;
import android.os.Bundle;
import android.os.Handler;
import android.util.Log;
import android.view.Display;

import com.cuanbo.dualscreenteachzhu.R;
import com.cuanbo.dualscreenteachzhu.base.App;
import com.cuanbo.dualscreenteachzhu.bean.AtyInfo;
import com.cuanbo.dualscreenteachzhu.bean.EventBusBean;
import com.cuanbo.dualscreenteachzhu.fragment.Camera2VideoFragment;
import com.cuanbo.dualscreenteachzhu.receiver.MyReceiver;
import com.cuanbo.dualscreenteachzhu.utils.ActivityUtil;
import com.cuanbo.dualscreenteachzhu.utils.BroadcastUtil;

import org.greenrobot.eventbus.EventBus;

public class MainActivity extends Activity implements MyReceiver.MyReceiverListener, DisplayManager.DisplayListener {
    public static boolean isCompare;
    public static boolean comment;
    private MyReceiver myReceiver;
    private DisplayManager mDisplayManager;
    private Camera2VideoFragment camera2VideoFragment;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        setContentView(R.layout.activity_main);
        init();
        if (null == savedInstanceState) {
            getFragmentManager().beginTransaction()
                    .replace(R.id.container, camera2VideoFragment=Camera2VideoFragment.newInstance())
                    .commit();

        }

    }

    private void init(){
        initView();
        mDisplayManager = (DisplayManager) getSystemService(DISPLAY_SERVICE);
        mDisplayManager.registerDisplayListener(this,new Handler());
        new Handler().postDelayed(new Runnable() {
            @Override
            public void run() {
                showHdmi();
            }
        },1000);
        registerReceiver(myReceiver=new MyReceiver(this),new IntentFilter("com.cuanbo.dralscreenteachzhu.action"));
    }

    private void showFuScreen(String pkgName,String cls) {
        Display[] displays = mDisplayManager.getDisplays(null);
        for (int i=0;i<displays.length;i++){
            if(displays[i].getDisplayId()!=0){
                startApp(displays[i].getDisplayId(),pkgName,cls);
                return;
            }
        }
    }

    private void startApp(int displayId, String packageName, String className) {
        ActivityOptions options = ActivityOptions.makeBasic();
        options.setLaunchDisplayId(displayId);
        Intent intent = new Intent();
        ComponentName componentName = new ComponentName(packageName, className);
        intent.setComponent(componentName);
        intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
        try {
            startActivity(intent, options.toBundle());
        } catch (ActivityNotFoundException e) {
            e.printStackTrace();
        }
    }

    @Override
    protected void onResume() {
        super.onResume();
        BroadcastUtil.sendBroadcast();
        showFuScreen("com.cuanbo.dualscreenteachfu","com.cuanbo.dualscreenteachfu.activity.MainActivity");
    }

    private void initView() {

    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        unregisterReceiver(myReceiver);
    }

    @Override
    public void drawBoard() {
        EventBus.getDefault().post(new EventBusBean(EventBusBean.MODE_WHITE_BOARD));
        EventBus.getDefault().post(new EventBusBean(EventBusBean.BUTTON_COMMENTS_NO));
    }

    @Override
    public void comment() {
        EventBus.getDefault().post(new EventBusBean(EventBusBean.MODE_COMMENT));
        EventBus.getDefault().post(new EventBusBean(EventBusBean.BUTTON_COMMENTS_NO));
        //showHdmi();
    }

    @Override
    public void hdmi() {
        EventBus.getDefault().post(new EventBusBean(EventBusBean.BUTTON_COMMENTS_OFF));
    }

    @Override
    public void sign(){
        camera2VideoFragment.sign();

    }

    @Override
    public void closeSign(){
        camera2VideoFragment.closeSign();
    }

    @Override
    public void capture() {
        Log.i("luohao","截屏");
        camera2VideoFragment.takePicture();
    }

    @Override
    public void showHdmi(){

    }

    @Override
    public void compare() {
        camera2VideoFragment.compare();
    }

    @Override
    public void compareImg1(String fileName) {
        camera2VideoFragment.compareImg1(fileName);
    }

    @Override
    public void compareImg2(String fileName) {
        camera2VideoFragment.compareImg2(fileName);

    }

    @Override
    public void startRecordingVideo() {
        camera2VideoFragment.startRecordingVideo();
    }

    @Override
    public void stopRecordingVideo() {
        camera2VideoFragment.stopRecordingVideo();
    }

    @Override
    public void playVideo(String path) {
        AtyInfo atyInfo =App.getActivity(PlayerActivity.class);
        if(atyInfo!=null&&atyInfo.getState()==AtyInfo.STATE_RESUME){
            atyInfo.getAty().finish();
        }
        ActivityUtil.open(this,PlayerActivity.class,"path",path);
    }

    @Override
    public void closeVideo() {
        AtyInfo atyInfo =App.getActivity(PlayerActivity.class);
        if(atyInfo!=null&&atyInfo.getState()==AtyInfo.STATE_RESUME){
            atyInfo.getAty().finish();
        }
    }

    @Override
    public void onDisplayAdded(int displayId) {

    }

    @Override
    public void onDisplayRemoved(int displayId) {

    }

    @Override
    public void onDisplayChanged(int displayId) {

    }
}